package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.button.MaterialButton;

import cn.lrwjz.androidguide.R;

/**
 * Menu示例Activity
 * 展示Material Design菜单的使用方法
 */
public class MenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            // 返回箭头
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Menu 示例");
        }

        // 注册上下文菜单（绑定到控件上，长按显示）
        registerForContextMenu(findViewById(R.id.contextMenuText));

        // 设置弹出菜单按钮点击事件
        MaterialButton btnPopupMenu = findViewById(R.id.btnPopupMenu);
        btnPopupMenu.setOnClickListener(v -> showPopupMenu(v));

        // 设置级联菜单按钮点击事件
        MaterialButton btnCascadeMenu = findViewById(R.id.btnCascadeMenu);
        btnCascadeMenu.setOnClickListener(v -> showCascadeMenu(v));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 加载选项菜单
        getMenuInflater().inflate(R.menu.menu_options, menu);
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        // 加载上下文菜单
        getMenuInflater().inflate(R.menu.menu_context, menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        // 处理选项菜单点击事件
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            finish();
            return true;
        } else if (itemId == R.id.action_settings) {
            showToast("点击了设置");
            return true;
        } else if (itemId == R.id.action_search) {
            showToast("点击了搜索");
            return true;
        } else if (itemId == R.id.action_about) {
            showToast("点击了关于");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        // 处理上下文菜单点击事件
        int itemId = item.getItemId();
        if (itemId == R.id.action_edit) {
            showToast("点击了编辑");
            return true;
        } else if (itemId == R.id.action_delete) {
            showToast("点击了删除");
            return true;
        } else if (itemId == R.id.action_share) {
            showToast("点击了分享");
            return true;
        }
        return super.onContextItemSelected(item);
    }

    private void showPopupMenu(View view) {
        PopupMenu popup = new PopupMenu(this, view);
        // 设置菜单xml
        popup.getMenuInflater().inflate(R.menu.menu_context, popup.getMenu());
        // 设置菜单显示位置
        popup.setGravity(Gravity.CENTER);
        popup.setOnMenuItemClickListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.action_edit) {
                showToast("点击了编辑");
                return true;
            } else if (itemId == R.id.action_delete) {
                showToast("点击了删除");
                return true;
            } else if (itemId == R.id.action_share) {
                showToast("点击了分享");
                return true;
            }
            return false;
        });
        popup.show();
    }

    private void showCascadeMenu(View view) {
        PopupMenu popup = new PopupMenu(this, view);
        popup.getMenuInflater().inflate(R.menu.menu_cascade, popup.getMenu());
        popup.setOnMenuItemClickListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.action_new) {
                showToast("点击了新建");
                return true;
            } else if (itemId == R.id.action_open) {
                showToast("点击了打开");
                return true;
            } else if (itemId == R.id.action_save) {
                showToast("点击了保存");
                return true;
            } else if (itemId == R.id.action_cut) {
                showToast("点击了剪切");
                return true;
            } else if (itemId == R.id.action_copy) {
                showToast("点击了复制");
                return true;
            } else if (itemId == R.id.action_paste) {
                showToast("点击了粘贴");
                return true;
            }
            return false;
        });
        popup.show();
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
} 